const data = [
  {
    id: 1,
    name: "0",
    children: [
      { id: 2, name: "0-1", children: [{ id: 6, name: "0-1-1" }] },
      { id: 7, name: "0-2", children: [{ id: 8, name: "0-2-1" }] },
    ],
  },
  {
    id: 3,
    name: "1",
    children: [{ id: 4, name: "1-1", children: [{ id: 5, name: "1-1-1" }] }],
  },
];
/**
 * 深度优先遍历
 * @param {object || any[]} node 递归数据
 * @param {any[]} nodeList 保存最终结果
 * @returns any[] nodeList
 */
const deepForeach = (node, nodeList = []) => {
  // 如果有node节点则执行主逻辑
  if (node) {
    // 导出结果填入节点数据
    nodeList.push({ id: node.id, name: node.name });
    // 如果有数据且有数据长度则继续递归
    node.children &&
      node.children.length &&
      node.children.forEach((item) => deepForeach(item, nodeList));
  }
  return nodeList;
};

console.log(data.map((item) => deepForeach(item)));
